//
//  TicketInfoDataModel.m
//  ShowTimeApp
//
//  Created by Ruby on 11/6/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "TicketInfoDataModel.h"
#import "CinemaService.h"
#import "Cinema.h"
#import "CinemaDataModel.h"

@implementation TicketInfoDataModel

-(id)initTicketInfoDataModel{
    [self initBaseDataModel];
    //[self addObjects];
    return self;
}

-(TicketInfo*)getDetailByID:(int)ID{
    TicketInfo* tk = nil;
    NSString* condition=[NSString stringWithFormat:@"ID==%@",[NSNumber numberWithInt:ID]];
    NSPredicate* predicate = [NSPredicate predicateWithFormat:condition];
    tk= (TicketInfo*)[self fetchObject:@"TicketInfo" byPredicate:predicate];
    return tk;
}

-(void)insertObject:(id)ticketinfo{
    CinemaService_TicketInfoEntity *newTK =(CinemaService_TicketInfoEntity*)ticketinfo;
    TicketInfo *t = (TicketInfo*)[NSEntityDescription insertNewObjectForEntityForName:@"TicketInfo" inManagedObjectContext:self.managedObjectContext];
    t.ID=[newTK ID_];
    t.ImageUrl=[newTK ImageUrl];
    t.Version=[newTK Version];
    t.DayOfWeek=[newTK DayOfWeek];
    t.StartedTime=[newTK StartedTime];
    t.EndedTime=[newTK EndedTime];
    t.Fare=[newTK Fare];
    t.Type=[newTK Type];
    t.Cinema=[CinemaDataModel convertFromCinemaService_CinemaEntity:[newTK CinemaFK] TodbCinemaEntity:t.Cinema];
    t.CinemaID=[[newTK CinemaFK] ID_];
    t.Subject=[newTK Subject];
}

-(void)insertObjects:(NSMutableArray *)ticketInfoList{
    for (id t in ticketInfoList){
        [self insertObject:t];
    }
}

-(NSMutableArray*)getAllTickeInfoOfCinemaSortByIDAscending:(int)cinemaID{
    // Add a sort descriptor
	NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"ID" ascending:YES selector:nil];
    
    // Add a predicate
    NSString* condition= [NSString stringWithFormat:@"CinemaID==%@",[NSNumber numberWithInt:cinemaID]];
    NSPredicate* predicate = [NSPredicate predicateWithFormat:condition];
    
    // Goi ham o lop cha
    [self fetchObjects:@"TicketInfo" sortBy:sortDescriptor byPredicate:predicate];
    return (NSMutableArray*)fetchResultController.fetchedObjects; 
}

-(void)deleteAll{
    // Add a sort descriptor
	NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"ID" ascending:YES selector:nil];
    [self fetchObjects:@"TicketInfo" sortBy:sortDescriptor];
    [self removeObjects];
}

+(TicketInfo*)convertFromCinemaService_TicketInfoEntity:(CinemaService_TicketInfoEntity *)svTicketInfoEntity TodbTicketInfoEntity:(TicketInfo *)dbTicketInfoEntity{
    CinemaService_TicketInfoEntity *newTK =(CinemaService_TicketInfoEntity*)svTicketInfoEntity;
    dbTicketInfoEntity.ID=[newTK ID_];
    dbTicketInfoEntity.ImageUrl=[newTK ImageUrl];
    dbTicketInfoEntity.Version=[newTK Version];
    dbTicketInfoEntity.DayOfWeek=[newTK DayOfWeek];
    dbTicketInfoEntity.StartedTime=[newTK StartedTime];
    dbTicketInfoEntity.EndedTime=[newTK EndedTime];
    dbTicketInfoEntity.Fare=[newTK Fare];
    dbTicketInfoEntity.Type=[newTK Type];
    dbTicketInfoEntity.Cinema=[CinemaDataModel convertFromCinemaService_CinemaEntity:[newTK CinemaFK] TodbCinemaEntity:dbTicketInfoEntity.Cinema];
    dbTicketInfoEntity.CinemaID=[[newTK CinemaFK] ID_];
    dbTicketInfoEntity.Subject=[newTK Subject];
    return dbTicketInfoEntity;
}

-(void)output:(NSMutableArray *)ticketinfoList{
    if(ticketinfoList.count>0)
    {
        for (id tk in ticketinfoList){ 
            TicketInfo* t = (TicketInfo*)tk;
            Cinema* c = t.Cinema;
            NSLog(@"%@ - %@ - %@ - %@ - %@ - %@ - %@",t.Subject,t.Type,t.Fare,t.DayOfWeek,t.StartedTime,t.EndedTime,t.CinemaID);
            NSLog(@"%@",c.Name);
        }
    }    
}
@end
